package main.java.com.example;

import org.hyperledger.fabric.shim.ChaincodeBase;
import org.hyperledger.fabric.shim.ChaincodeStub;
import org.hyperledger.fabric.shim.ResponseUtils;

public class CC extends ChaincodeBase {

    @Override
    public Response init(ChaincodeStub chaincodeStub) {
        return null;
    }

    @Override
    public Response invoke(ChaincodeStub stub) {
        try {
            String func = stub.getFunction();

            if (func.equals("Put")) {
                return put(stub);
            } else if (func.equals("Get")) {
                return get(stub);
            }

            return ResponseUtils.newErrorResponse("Invalid invoke function name.");
        } catch (Throwable e) {
            return ResponseUtils.newErrorResponse(e);
        }
    }

    private Response put(ChaincodeStub stub) {
        String str = String.valueOf(Math.random());
        stub.putStringState("key"+str, "value"+str);
        String returnMessage = "execute chaincode successfully";
        return ResponseUtils.newSuccessResponse(returnMessage);
    }

    private Response get(ChaincodeStub stub) {
        String result = stub.getStringState("key");
        return ResponseUtils.newSuccessResponse(result);
    }


    public static void main(String[] args) {
        CC cc = new CC();
        cc.start(args);
    }
}
